<template>
  <div class="edit-popup">
    <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
      <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
        <el-form-item label="编码开头" prop="prefix">
          <el-input v-model="formData.prefix" placeholder="例如：NO."></el-input>
        </el-form-item>
        <el-form-item label="编码" prop="num">
          <div style="display: flex">
            <el-form-item>
              <el-input-number v-model="formData.beginNum" placeholder="请输入编码"></el-input-number>
            </el-form-item>
            <span style="margin: 0 20px">至</span>
            <el-form-item>
              <el-input-number v-model="formData.endNum" placeholder="请输入编码"></el-input-number>
            </el-form-item>
          </div>
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="formData.num" disabled></el-input>
        </el-form-item>
      </el-form>
      <div class="btn-group">
        <span>编码样式：{{ formData.prefix
        }}.{{ formData.beginNum.toString().padStart(5, '0') }}至{{
            formData.prefix
          }}.{{ formData.endNum.toString().padStart(5, '0') }}</span>
        <span>数量：{{ codeNum }}</span>
      </div>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { computed, reactive, ref, shallowRef, watch } from 'vue'
import feedback from '@/utils/feedback'
import { addApi, detailApi, editApi } from '@/api/activity/codeBatch'
import { en } from 'element-plus/es/locale/index.mjs'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑编码' : '新增编码'
})

const formData = reactive({
  id: null,
  num: 0,
  prefix: 'NO',
  beginNum: 1,
  endNum: 10,
})

const formRules = ref({
  num: [
    {
      required: true,
      message: '请输入数量',
      trigger: ['blur'],
    },
  ],
  beginNum: [
    {
      required: true,
      message: '请输入编码',
      trigger: ['blur'],
    }, {
      validator: (rule: any, value: number, callback: (error?: Error) => void) => {
        if (value >= formData.endNum) {
          callback(new Error('结束编码必须大于开始编码'));
        } else {
          callback();
        }
      },
      trigger: ['blur'],
    },
  ],
  endNum: [
    {
      required: true,
      message: '请输入编码',
      trigger: ['blur'],
    }, {
      validator: (rule: any, value: number, callback: (error?: Error) => void) => {
        if (value <= formData.beginNum) {
          callback(new Error('结束编码必须大于开始编码'));
        } else {
          callback();
        }
      },
      trigger: ['blur'],
    },
  ],
})
const codeNum = computed(() => {
  return formData.endNum - formData.beginNum + 1
})
watch(
  codeNum,
  () => {
    formData.num = codeNum.value
  },
  { immediate: true },
)
const handleSubmit = async () => {
  await formRef.value?.validate()
  if (formData.num < 1) {
    feedback.msgError('请输入正确的数量')
    return
  }
  mode.value == 'edit' ? await editApi(formData) : await addApi(formData)
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}
const setFormData = async (row: any) => {
  const data = await detailApi({
    id: row.id,
  })
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}
const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
})
</script>
